import './assets/main.scss'
import './style.css'
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import App from './App.vue'
import router from './router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import store from './store'
import 'element-plus/dist/index.css'
import locale from 'element-plus/dist/locale/zh-cn.js'
import PanelHead from '@/components/panelHead.vue'
import {createPersistedState} from 'pinia-persistedstate-plugin'
import { createPinia } from 'pinia'
const pinia = createPinia()
const persist = createPersistedState()
pinia.use(persist)
router.beforeEach((to, from) => {
  const token = localStorage.getItem('min_token');
  console.log('路由守卫检查 token:', token, '目标路径:', to.path);

  if (!token && to.path !== '/user/login') {  // 用户未登录且访问的不是登录页
      //console.log('没有 token，重定向到 /login');
      return '/user/login';
  }
  if (token && to.path === '/user/login') {  // 已登录但访问的是登录页
      //console.log('已登录，重定向到首页');
      return '/';
  }
  //console.log('放行路由');
  return true;
});

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
  }
app.component('PanelHead',PanelHead)
app.use(router)
//store挂载
app.use(store)
app.use(ElementPlus,{locale})
app.use(pinia)
app.mount('#app')
